package controllers

import (
	"github.com/astaxie/beego"
	lsql "jujiu/data/persist/mysql"
	"log"
	"strconv"
	"strings"
)

type QueryController struct {
	beego.Controller
}

type DeviceBaseInfoRsp struct {
	SerildId    string
	DeviceNo    string
	DeviceId    string
	Imei        string
	Iccid       string
	MainRomVer  string
	SubRomVer   string
	ModuleVer   string
	Sn          string
	CCid        string
	SlotDisable string
	Online      string
}

func convertRsp(baseInfos [] lsql.DeviceBaseInfo) *DeviceBaseInfoRsp {
	rsp := &DeviceBaseInfoRsp{}

	for _, v := range baseInfos {

		if rsp.SerildId == "" {
			rsp.SerildId = strconv.FormatInt(v.SerialId, 10)
		} else {
			rsp.SerildId = rsp.SerildId + "," + strconv.FormatInt(v.SerialId, 10)
		}
		if rsp.DeviceId == "" {
			rsp.DeviceId = strconv.FormatInt(v.DeviceId, 10)
		} else {
			rsp.DeviceId = rsp.DeviceId + "," + strconv.FormatInt(v.DeviceId, 10)
		}
		if rsp.DeviceNo == "" {
			rsp.DeviceNo = v.DeviceNo
		} else {
			rsp.DeviceNo = rsp.DeviceNo + "," + v.DeviceNo
		}

		if rsp.Imei == "" {
			rsp.Imei = v.Imei
		} else {
			rsp.Imei = rsp.Imei + "," + v.Imei
		}

		if rsp.Iccid == "" {
			rsp.Iccid = v.Iccid
		} else {
			rsp.Iccid = rsp.Iccid + "," + v.Iccid
		}

		if rsp.MainRomVer == "" {
			rsp.MainRomVer = v.MainRomVer
		} else {
			rsp.MainRomVer = rsp.MainRomVer + "," + v.MainRomVer
		}

		if rsp.SubRomVer == "" {
			rsp.SubRomVer = v.SubRomVer
		} else {
			rsp.SubRomVer = rsp.SubRomVer + "," + v.SubRomVer
		}

		if rsp.ModuleVer == "" {
			rsp.ModuleVer = v.ModuleVer
		} else {
			rsp.ModuleVer = rsp.ModuleVer + "," + v.ModuleVer
		}
		if rsp.Sn == "" {
			rsp.Sn = v.Sn
		} else {
			rsp.Sn = rsp.Sn + "," + v.Sn
		}
		if rsp.CCid == "" {
			rsp.CCid = v.CCid
		} else {
			rsp.CCid = rsp.CCid + "," + v.CCid
		}

		ds := lsql.GetDbResourceByCCid(v.CCid)
		slotMapper := lsql.NewDeviceSlotMapper(ds)
		cabinetStateMapper := lsql.NewDeviceCabinetStateMapper(ds)
		slotStates := slotMapper.QueryByDeviceId(v.DeviceId);
		for _, v := range slotStates {
			if v.Useable == 1 {
				if rsp.SlotDisable == "" {
					rsp.SlotDisable = strconv.FormatInt(v.SlotId, 10)
				} else {
					rsp.SlotDisable = rsp.SlotDisable + "," + strconv.FormatInt(v.SlotId, 10)
				}

			}
		}
		cabinetState := cabinetStateMapper.QueryByDeviceId(v.DeviceId)

		if cabinetState != nil {
			if rsp.Online == "" {
				rsp.Online = cabinetState.OnlineFlag
			} else {
				rsp.Online = rsp.Online + "," + cabinetState.OnlineFlag
			}
		}

	}
	return rsp;
}

func (q *QueryController) QueryDevice() {
	deviceId := q.GetString("deviceId")
	if deviceId == "" {
		log.Println("获取参数错误！")
		q.Ctx.WriteString("参数为空")
		return
	}
	clinet := lsql.DataSourceClientMap["cn_real"]
	mapper := lsql.NewDeviceBaseMapper(&clinet)
	deviceInfo := &lsql.DeviceBaseInfo{}
	infos := make([]lsql.DeviceBaseInfo, 2)
	if len(deviceId) == 4 {
		infos = mapper.QueryByDeviceNo2(deviceId)
	} else if strings.HasPrefix(deviceId, "96") {
		did, err := strconv.ParseInt(deviceId, 10, 64)
		if err != nil {
			q.Ctx.WriteString("设备id解析失败")
			return
		}
		deviceInfo = mapper.QueryDeviceBaseInfoByDeviceId(did)
		infos = append(infos, *deviceInfo)
	} else if strings.HasPrefix(deviceId, "86") {
		deviceInfo = mapper.QueryByImei(deviceId)
		infos = append(infos, *deviceInfo)
	} else if strings.HasPrefix(deviceId, "89") {
		deviceInfo = mapper.QueryByIccid(deviceId)
		infos = append(infos, *deviceInfo)
	} else {
		deviceInfo = mapper.QueryBySn(deviceId)
		infos = append(infos, *deviceInfo)
	}
	if deviceInfo == nil {
		infos = mapper.QueryByDeviceNo2(deviceId)
	}

	if deviceInfo == nil {
		q.Ctx.WriteString("设备不存在")
		return
	}
	rsp := convertRsp(infos)
	//jsonStr, err := json.Marshal(deviceBaseInfo)
	q.Data["json"] = rsp
	q.ServeJSON()
	return
}
